///  The code starts from here ///
clear all
cd "/RND/"
use "/RND/Initial.dta", clear

merge m:1 year using "/RND/CPI.dta", 
drop  _merge

// Merge Hoberg and Phillips  ///
destring gvkey, replace
sort firmid
sort gvkey year
merge m:1 gvkey year using "/RND/HobergPhillips.dta", 
drop if _merge ==2
drop  _merge
//Markup data from Compustat//
merge m:1 gvkey year using "/RND/salecogs.dta", 
drop if _merge ==2
drop  _merge

// limit the data to 2009 to 2018//
drop if year <2009 | year > 2018

// generate continuous variables //
gen frdemp1 = (WRDEM_A -RDEMP_A)/(WRDEM_A)
gen frd1 = (WOTRD_A - TOTRD_A)/(WOTRD_A)
gen patent = 1000*APPLY_A/WOTRD_A
gen lnemp = ln(firmsize_emp)
gen lnsale = ln(SALES_A/cpiindex08)
gen lnage = ln(firmage_pay)
gen lncong = ln(conglomerate)
gen salemp = SALES_A/EMP_A
gen markup = elast*sale/cogs  //elasticity values are from the literature
//De Loecker, J., J. Eeckhout, and G. Unger, 2020.  The Rise of Market Power and the Macroeconomic Implications.  Quarterly Journal of Economics, 135(2): 561–644.//
// generate dummy variables //
gen basicdum = 0 if BASTD_A ==0
replace basicdum = 1 if BASTD_A >0 & BASTD_A ~=.
gen acqip1 = 0 
replace acqip1 = 1 if IPE_A ==2
gen death = 0
replace death = 1 if firm_lastyear_pay == year
gen startfrd = 0
replace startfrd = 1 if frd1 > 0 & frd1[_n-1] ==0 & frd1 ~=. & firmid == firmid[_n-1]
gen ydum = 0
replace ydum = 1 if year >= 2014 & year ~=.


/// winsorize the sample///
winsor2 frdemp1 frd1 patent tnic3tsimm tnic3hhi markup lnemp lnsale lnage lncong salemp 

/// Form the lagged variables///
sort firmid year
destring firmid, replace
xtset firmid year
/// form some summary statistics controls ///
gen llsale = l.lnsale_w
gen llemp = l.lnemp_w
gen lage  = l.lnage_w
gen lconglo = l.lncong_w
gen lsalemep = l.salemp_w

/// Save the sample ////
save del.dta, replace

// Form the variables in 2009//
keep if year ==2009
//Only US Employee firms//
gen foreignempdumcon = 0 
replace foreignempdumcon = 1 if WEMP_A == EMP_A & WEMP_A ~=. 
// find the financially constrained firms //
egen sizeq = xtile(firmsize_emp) , n(2) 
egen sizeq1 = xtile(firmage_pay) , n(2) 
gen finconstrcont = 0 
replace finconstrcont = 1 if   sizeq ==1 & sizeq1 ==1
// Find the private firms//
gen public1 = 0 if public ==1
replace public1 = 1 if public ==0
///H1 B dependence ///
gen h1b09= 0
replace h1b09= VISA_A/RDEMP_A 
winsor2 h1b09

/// Form the Sample from here ///
// Merge with the constructed variables//
sort firmid
merge m:1 firmid using "/RND/del.dta", 
drop if _merge ~=3
drop  _merge
drop if llsale==. |  lsalemep ==. | llemp ==. | lage==. | lconglo ==. | lsalemep ==.
drop if year ==2009
/// set the panel///
sort firmid year
destring firmid, replace
xtset firmid year


/////////////////////////////////////////////
// start the analysis from here //

/// Table 2: Summary Statistics of dependent variable /// 
summarize  frdemp1_w frd1_w startfrd basicdum patent_w acqip1 death tnic3tsimm_w tnic3hhi_w markup_w

/// Table 3: Summary Statistics of independent variable /// 
summarize foreignempdumcon finconstrcont public1 h1b09_w llsale_w llemp_w lage_w lconglo_w lsalemep_w

//Tables 4 to 10: Regressions  //
foreach y in frdemp1_w frd1_w startfrd basicdum patent_w acqip1 death  {
//Panel A//
local cont  llsale_w llemp_w lage_w lconglo_w lsalemep_w , absorb(naics3#year  firmid) vce(cluster  firmid year ) keepsingleton
reghdfe  `y'  foreignempdumcon##ydum  `cont'  
reghdfe  `y'  finconstrcont#ydum  `cont'  
reghdfe  `y'  public1##ydum  `cont'  
reghdfe  `y'  c.h1b09##ydum  `cont'  

//Panel B//
reghdfe  `y'  foreignempdumcon##ydum#c.h1b09_w  `cont' 
reghdfe  `y'  finconstrcont#ydum##c.h1b09_w  `cont' 
reghdfe  `y'  public1##ydum#c.h1b09_w  `cont'  
}

//Tables 11 to 13:  Regressions with only publicly traded firms//
foreach y in tnic3tsimm_w tnic3hhi_w markup_w {
//Panel A//
local cont  llsale_w llemp_w lage_w lconglo_w lsalemep_w , absorb(naics3#year  firmid) vce(cluster  firmid year ) keepsingleton
reghdfe  `y'  foreignempdumcon##ydum  `cont'  
reghdfe  `y'  finconstrcont#ydum  `cont'  
reghdfe  `y'  c.h1b09##ydum  `cont'  

//Panel B//
reghdfe  `y'  foreignempdumcon##ydum#c.h1b09_w  `cont' 
reghdfe  `y'  finconstrcont#ydum##c.h1b09_w  `cont' 
}





